home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / bresenhm.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  4.9 KB  |  208 lines

  1. /* $Id: bresenhm.h,v 1.3 1996/10/17 03:24:20 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.0
  6.  * Copyright (C) 1995-1996  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: bresenhm.h,v $
  26.  * Revision 1.3  1996/10/17 03:24:20  brianp
  27.  * use 7 fractional bits instead of 8 in BRESENHAM_Z() macro
  28.  *
  29.  * Revision 1.2  1996/09/15 14:18:10  brianp
  30.  * now use GLframebuffer and GLvisual
  31.  *
  32.  * Revision 1.1  1996/09/13 01:38:16  brianp
  33.  * Initial revision
  34.  *
  35.  */
  36.  
  37.  
  38. /*
  39.  * A macro which executes Bresenham's line drawing algorithm.  The
  40.  * previously defined BRESENHAM_PLOT macro is then used to 'plot' pixels.
  41.  */
  42.  
  43.  
  44. #ifndef BRESENHAM_H
  45. #define BRESENHAM_H
  46.  
  47.  
  48. #include "types.h"
  49.  
  50.  
  51.  
  52. /* TODO:  combine these macros to make a linetemp.h file like tritemp.h */
  53.  
  54.  
  55. /*
  56.  * Bresenham's line algorithm.
  57.  */
  58. #define BRESENHAM( x1, y1, x2, y2 )    \
  59. {                    \
  60.    GLint dx, dy, xf, yf, ta, tb, tt, i;    \
  61.    if (x1!=x2 || y1!=y2) {        \
  62.       if (x2>x1) {            \
  63.          dx = x2-x1;            \
  64.          xf = 1;            \
  65.       }                    \
  66.       else {                \
  67.          dx = x1-x2;            \
  68.          xf = -1;            \
  69.       }                    \
  70.       if (y2>y1) {            \
  71.          dy = y2-y1;            \
  72.          yf = 1;            \
  73.       }                    \
  74.       else {                \
  75.          dy = y1-y2;            \
  76.          yf = -1;            \
  77.       }                    \
  78.       if (dx>dy) {            \
  79.          ta = dy+dy;            \
  80.          tt = ta-dx;            \
  81.          tb = tt-dx;            \
  82.          for (i=0;i<=dx;i++) {        \
  83.         BRESENHAM_PLOT( x1, y1 )    \
  84.             x1 += xf;            \
  85.             if (tt<0) {            \
  86.                tt += ta;        \
  87.             }                \
  88.             else {            \
  89.                tt += tb;        \
  90.                y1 += yf;        \
  91.             }                \
  92.          }                \
  93.       }                    \
  94.       else {                \
  95.          ta = dx+dx;            \
  96.          tt = ta-dy;            \
  97.          tb = tt-dy;            \
  98.          for (i=0;i<=dy;i++) {        \
  99.         BRESENHAM_PLOT( x1, y1 )    \
  100.             y1 += yf;            \
  101.             if (tt<0) {            \
  102.                tt += ta;        \
  103.             }                \
  104.             else {            \
  105.                tt += tb;        \
  106.                x1 += xf;        \
  107.         }                \
  108.          }                \
  109.       }                    \
  110.    }                    \
  111. }
  112.  
  113.  
  114.  
  115.  
  116. /*
  117.  * Bresenham's line algorithm with Z interpolation.
  118.  * Z interpolation done with fixed point arithmetic, 7 fraction bits.
  119.  */
  120. #define BRESENHAM_Z( ctx, x1, y1, z1, x2, y2, z2 )    \
  121. {                            \
  122.    GLint dx, dy, xstep, ystep, ta, tb, tt, i;        \
  123.    GLint dz, dzdx, dzdy;                \
  124.    GLdepth *zptr;                    \
  125.    if (x1!=x2 || y1!=y2) {            \
  126.       z1 = z1 << 7;                \
  127.       z2 = z2 << 7;                \
  128.       if (x2>x1) {                \
  129.          dx = x2-x1;                \
  130.          xstep = 1;                \
  131.      dzdx = 1;                \
  132.       }                        \
  133.       else {                    \
  134.          dx = x1-x2;                \
  135.          xstep = -1;                \
  136.      dzdx = -1;                \
  137.       }                        \
  138.       if (y2>y1) {                \
  139.          dy = y2-y1;                \
  140.          ystep = 1;                \
  141.      dzdy = ctx->Buffer->Width;        \
  142.       }                        \
  143.       else {                    \
  144.          dy = y1-y2;                \
  145.          ystep = -1;                \
  146.      dzdy = -ctx->Buffer->Width;        \
  147.       }                        \
  148.       zptr = Z_ADDRESS(ctx,x1,y1);        \
  149.       if (dx>dy) {                \
  150.          dz = (z2-z1)/dx;            \
  151.          ta = dy+dy;                \
  152.          tt = ta-dx;                \
  153.          tb = tt-dx;                \
  154.          for (i=0;i<=dx;i++) {            \
  155.             GLdepth z = z1>>7;            \
  156.         BRESENHAM_PLOT( x1, y1, z, zptr )    \
  157.             x1 += xstep;            \
  158.         zptr += dzdx;            \
  159.             if (tt<0) {                \
  160.                tt += ta;            \
  161.             }                    \
  162.             else {                \
  163.                tt += tb;            \
  164.                y1 += ystep;            \
  165.            zptr += dzdy;            \
  166.             }                    \
  167.         z1 += dz;                \
  168.          }                    \
  169.       }                        \
  170.       else {                    \
  171.          dz = (z2-z1)/dy;            \
  172.          ta = dx+dx;                \
  173.          tt = ta-dy;                \
  174.          tb = tt-dy;                \
  175.          for (i=0;i<=dy;i++) {            \
  176.             GLdepth z = z1>>7;            \
  177.         BRESENHAM_PLOT( x1, y1, z, zptr )    \
  178.             y1 += ystep;            \
  179.         zptr += dzdy;            \
  180.             if (tt<0) {                \
  181.                tt += ta;            \
  182.             }                    \
  183.             else {                \
  184.                tt += tb;            \
  185.                x1 += xstep;            \
  186.            zptr += dzdx;            \
  187.         }                    \
  188.         z1 += dz;                \
  189.          }                    \
  190.       }                        \
  191.    }                        \
  192. }
  193.  
  194.  
  195.  
  196. extern GLuint gl_bresenham( GLcontext* ctx,
  197.                             GLint x1, GLint y1, GLint x2, GLint y2,
  198.                 GLint x[], GLint y[] );
  199.  
  200.  
  201. extern GLuint gl_stippled_bresenham( GLcontext* ctx,
  202.                                      GLint x1, GLint y1, GLint x2, GLint y2,
  203.                      GLint x[], GLint y[], GLubyte mask[] );
  204.  
  205.  
  206.  
  207. #endif
  208.